package com.hyx.api.controller.pc_admin;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.annotation.RepeatSubmit;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;
import com.hyx.api.domain.bo.UserBo;
import com.hyx.api.domain.vo.AppUserVo;
import com.hyx.api.service.pc_admin.IUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 用户列表Controller
 *
 * @author HK
 * @date 2023-10-23
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/user")
public class UserController extends BaseController
{
    private final IUserService userService;

    /**
     * 查询用户列表列表
     */
    @PreAuthorize("@ss.hasPermi('api:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserBo bo)
    {
        startPage();
        List<AppUserVo> list = userService.selectUserList(bo);
        return getDataTable(list);
    }

    /**
     * 导出用户列表列表
     */
    @PreAuthorize("@ss.hasPermi('api:user:export')")
    @Log(title = "用户列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserBo bo)
    {
        List<AppUserVo> list = userService.selectUserList(bo);
        ExcelUtil<AppUserVo> util = new ExcelUtil<>(AppUserVo.class);
        util.exportExcel(response, list, "用户列表数据");
    }

    /**
     * 获取用户列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('api:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userService.selectUserById(id));
    }

    /**
     * 新增用户列表
     */
    @PreAuthorize("@ss.hasPermi('api:user:add')")
    @Log(title = "用户列表", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@Validated @RequestBody UserBo bo)
    {
        if(ObjectUtils.isEmpty(bo.getPassword()))
            return AjaxResult.error("密码不能为空");
        return toAjax(userService.insertUser(bo));
    }

    /**
     * 修改用户列表
     */
    @PreAuthorize("@ss.hasPermi('api:user:edit')")
    @Log(title = "用户列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody UserBo bo)
    {
        return toAjax(userService.updateUser(bo));
    }

    /**
     * 删除用户列表
     */
    @PreAuthorize("@ss.hasPermi('api:user:remove')")
    @Log(title = "用户列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userService.deleteUserByIds(ids));
    }
}
